/*
 * Copyright 2020 Mike Penz
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mikepenz.iconics;

import ohos.agp.render.Paint;
import ohos.agp.utils.Color;

import java.util.HashMap;
import java.util.Map;

/**
 * Helper class to control applying state changes to colors and paint. Also for compact providing
 * into [AnimationProcessor][com.mikepenz.iconics.animation.IconicsAnimationProcessor]
 *
 * @author pa.gulko zTrap (28.11.2018)
 */
public class IconicsBrush<T extends Paint> {
    private final T paint;
    private int currentState;
    private Map<Integer, Color> colorsList = new HashMap<>();
    private static final float MAX_ALPHA = 255f;

    /**
     * 构造方法
     *
     * @param paint 画笔
     */
    public IconicsBrush(T paint) {
        this.paint = paint;
        this.paint.setAlpha(1);
    }

    public T getPaint() {
        return paint;
    }

    public Map<Integer, Color> getColorsList() {
        return colorsList;
    }

    /**
     * 设置颜色
     *
     * @param colorsList 颜色
     */
    public void setColorsList(Map<Integer, Color> colorsList) {
        for (Map.Entry<Integer, Color> entry : colorsList.entrySet()) {
            this.colorsList.put(entry.getKey(), entry.getValue());
        }
    }

    /**
     * 获取当前透明度
     *
     * @return 透明度值
     */
    public int getAlpha() {
        return (int) (paint.getAlpha() * MAX_ALPHA);
    }

    /**
     * 设置当前透明度
     *
     * @param alpha 透明度值
     */
    public void setAlpha(int alpha) {
        if (alpha >= 0 && alpha <= MAX_ALPHA) {
            this.paint.setAlpha(alpha / MAX_ALPHA);
        }
    }

    /**
     * 是否具备状态
     *
     * @return 是否具备状态
     */
    public boolean isStateful() {
        return !colorsList.isEmpty();
    }

    /**
     * 获取当前状态下的颜色值
     *
     * @return 当前状态下的颜色值
     */
    public int getColorForCurrentState() {
        return getColorForCurrentState(Color.TRANSPARENT.getValue());
    }

    private int getColorForCurrentState(int defaultColor) {
        Color color = colorsList.get(currentState);
        if (color == null) {
            return defaultColor;
        }
        return color.getValue();
    }

    /**
     * 刷新状态
     *
     * @param state 状态值
     * @return 是否刷新成功
     */
    public boolean applyState(int state) {
        this.currentState = state;
        final int colorForState = getColorForCurrentState();
        paint.setColor(new Color(colorForState));
        Color color = paint.getColor();
        if (color == null) {
            return true;
        }
        final int oldColor = color.getValue();
        return colorForState != oldColor;
    }
}
